package A_review.review0105;

public class Class2689 {

    public int punishmentNumber(int n) {
        int ans=0;
        for (int i = 1; i <=n ; i++) {
            if (backtracking(i*i+"",0,i)){
                ans+=i*i;
            }
        }
        return  ans;
    }


    public   boolean  backtracking(String s,int start,int target){

   if (start==s.length()){
       return  target==0;
   }
        for (int i = start; i <s.length(); i++) {
            int i1 = Integer.parseInt(s.substring(start, i + 1));
            if (target-i1<0){
                break;
            }
            if (backtracking(s,i+1,target-i1)){
                return  true;
            }

        }

        return false;

    }



}
